/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.barrientosjunior.blackup.action;

import br.com.barrientosjunior.blackup.MainManager;
import br.com.barrientosjunior.blackup.model.File;
import br.com.barrientosjunior.utils.Files;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 *
 * @author Junior
 */
public class ExecuteBackupAction implements BackupAction, Runnable{
    MainManager manager = null;
    

    public ExecuteBackupAction(MainManager manager) {
        this.manager = manager;
    }

    public void execute() throws IOException {
        manager.getMainFrame().getjProgressBar1().setMaximum(manager.getFiles().size());
        manager.getMainFrame().getjLabelStatusValue().setText("Realizando backups...");
        for (int i = 0; i < manager.getFiles().size(); i++) {
            manager.getMainFrame().getjProgressBar1().setValue(i + 1);
            File file = manager.getFiles().get(i);

            if(file.isEncrypted()){
                ExecuteEncryptedBackupAction backup = new ExecuteEncryptedBackupAction(manager, file);
                backup.execute();
            } else {
                ExecuteSimpleBackupAction backup = new ExecuteSimpleBackupAction(manager, file);
                backup.execute();
            }
        }
        manager.getMainFrame().getjProgressBar1().setValue(0);
        manager.getMainFrame().getjLabelStatusValue().setText("Backups terminados com sucesso.");
    }

    public void run(){
        try {
            this.execute();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(manager.getMainFrame(),
                    ex.getMessage(),
                    "Erro de backup",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
}
